//
//  HMNavigationViewController.swift
//  WeiBo
//
//  Created by Apple on 16/9/18.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
//  自定义导航控制器
class HMNavigationViewController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //  设置边缘手势代理
        self.interactivePopGestureRecognizer?.delegate = self
        
        
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //  当导航控制器里面的个数大于0 在做处理
        if viewControllers.count > 0 {
            
            //  如果控制器是测试控制器设置返回按钮和标题
            if viewController is HMTestViewController {
            
                
                //  将要进入第二个视图控制器，获取第一个视图控制器的标题
                var title = "返回"
                if viewControllers.count == 1 {
                    title = viewControllers.first!.title ?? title
                    
                    //  设置第二个控制器的作为按钮
                    viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
                    
                } else {
                    //  设置返回按钮
                    viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
                    
                }
                //  设置push进来的控制器的标题
                viewController.title = "当前显示是第\(viewControllers.count + 1)级控制器"
                
                
            }
            //  隐藏底部tabbar
            viewController.hidesBottomBarWhenPushed = true
            
        }
        
        super.pushViewController(viewController, animated: animated)
        
        
    }
    
    @objc fileprivate func backAction() {
        _ = popViewController(animated: true)
    }
    
    
    // MARK:    --
    //  是否处理开始点击的边缘手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //  如果是根视图那么不需要处理
        if viewControllers.count == 1 {
            return false
        }
        //  其它控制器需要处理
        return true
    }
}
